<template>
  <div class="page-main">
    <home-nav :userData="userinfo"></home-nav>
    <div class="container">
      <div class="head-content">
        <span class="bold-font">合同执行信息</span>
        <ul>
          <li @click="toDataChangeLog()"><span class="iconfont icon-wendang"></span>项目日志</li>
        </ul>
        <div class="btn-line">
          <a-button v-if="checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])" @click="updateTaskUser"
            >更新报工人员</a-button
          >
          <a-button
            v-if="checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
            class="hte-btn add-btn"
            type="primary"
            @click="supperSave"
            >保存</a-button
          >
          <!-- <a-button
            v-if="
              (task.currentHandlerUser == '1' || checkPermission(['WORK_REPORT_MANAGER', 'WORK_REPORT_ACCEPTER'])) &&
              (!payment.status || payment.status == '0' || payment.status == '3')
            "
            class="hte-btn add-btn"
            type="primary"
            @click="handleSave"
            >保存</a-button
          >
          <a-button
            v-if="
              (task.currentHandlerUser == '1' || checkPermission(['WORK_REPORT_MANAGER', 'WORK_REPORT_ACCEPTER'])) &&
              (!payment.status || payment.status == '0' || payment.status == '3')
            "
            class="hte-btn"
            @click="handleSubmit"
            >提交审批</a-button
          > -->
        </div>
      </div>
      <div class="content">
        <ProjecExecuteBase :project="project" :permission="permission" :signCompany="signCompany" />
        <ProjectExecutePay :contractId="project.contractId + ''" />
        <ProjectExecuteReport :projectId="projectId" />
        <RelpaceTaskHandlerUser
          :show="showRelpaceTaskHandlerUser"
          @close="closeRelpaceTaskHandlerUser"
          :permission="permission"
        />
      </div>
    </div>
    <dataChangeTimeLine ref="dataChangeTimeLine" />
  </div>
</template>
<script>
import { getAction, postAction } from '@/api/manage'
import ProjecExecuteBase from '@/views/workReport/project/projectExecuteBase'
import ProjectExecutePay from '@/views/workReport/project/projectExecutePay'
import ProjectExecuteReport from '@/views/workReport/project/projectExecuteReport'
import RelpaceTaskHandlerUser from '@/views/workReport/util/RelpaceTaskHandlerUser'
export default {
  components: {
    ProjecExecuteBase,
    ProjectExecutePay,
    ProjectExecuteReport,
    RelpaceTaskHandlerUser,
    HomeNav: () => import('@/components/layouts/HomeNav'),
    dataChangeTimeLine: () => import('@/views/dataChange/dataChangeTimeLine'),
  },
  data() {
    return {
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'ContractReportInfo',
      },
      projectId: this.$route.params && this.$route.params.projectId,
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      pagination: {
        total: 0,
        current: 1,
      },
      tabLoading: false,
      currPageIndex: 1,
      currPageSize: 10,
      urls: {
        project: '/workReport/project/info',
        updateSignUser: '/workReport/project/updateSignUser',
        edit: '/workReport/project/edit',
        myPermission: '/workReport/project/myPermission',
        loadSignCompany: '/hte/contract/loadSignCompany',
      },
      project: {},
      permission: [],
      showRelpaceTaskHandlerUser: false,
      signCompany:[]
    }
  },
  mounted() {
    this.fetchProject()
    this.fetchPermission()
    this.fetchSignCompany()
  },
  methods: {
    fetchPermission() {
      getAction(this.urls.myPermission, { projectId: this.projectId }).then((res) => {
        if (res.success) {
          this.permission = res.result
        }
      })
    },
    checkPermission(powers) {
      let hasPower = false
      for (let p of powers) {
        if (this.permission.indexOf(p) > -1) {
          hasPower = true
          break
        }
      }
      return hasPower
    },
    fetchProject() {
      getAction(this.urls.project + '/' + this.projectId, {}).then((res) => {
        if (res.success) {
          this.project = res.result
        }
      })
    },
    pageChange() {
      this.currPageIndex = page.current
      this.currPageSize = page.pageSize
      this.pagination.current = page.current
      // this.getConTaskListData()
    },
    handledDelFile(item) {},
    updateSignUser() {
      postAction(this.urls.updateSignUser, { projectId: this.project.id }).then((res) => {
        if (res.success) {
          this.$messageWorkReport.success(res.message)
          this.fetchProject()
        } else {
          this.$messageWorkReport.error(res.message)
        }
      })
    },
    fetchSignCompany() {
      getAction(this.urls.loadSignCompany, {}).then((res) => {
        if (res.code == 200) {
          this.signCompany = res.result
        } else {
          this.$messageWorkReport.warning('请求数据失败:' + res.msg)
        }
      })
    },
    supperSave() {
      if (this.signCompany) {
        let isCorrect = false
        this.signCompany.forEach((c) => {
          if (c.componyNo == this.project.signCompanyCode && c.componyName == this.project.signCompany) {
            isCorrect = true
          }
        })
        if (!isCorrect) {
          return this.$messageWorkReport.warning(
            '当前签订公司[' +
              this.project.signCompany +
              '/' +
              this.project.signCompanyCode +
              ']不正确，请[合同报工管理员]在合同页面进行修正'
          )
        }
      }
      postAction(this.urls.edit, this.project).then((res) => {
        if (res.success) {
          this.$messageWorkReport.success('保存成功')
        }
      })
    },
    updateTaskUser() {
      this.showRelpaceTaskHandlerUser = true
    },
    closeRelpaceTaskHandlerUser() {
      this.showRelpaceTaskHandlerUser = false
    },
    toDataChangeLog() {
      this.$refs.dataChangeTimeLine.open([this.projectId])
    },
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
.content {
  margin-top: 20px;
}
</style>